package com.mstore.service;

import java.util.ArrayList;

import java.util.List;

import com.mstore.dao.ExchangeLogsDao;
import com.mstore.pojo.ExchangeLog;

public class ExchangeLogsService {

	private ExchangeLogsDao exchangeLogsDao;

	// 添加兑换商品
	public void addAddExlogs(ExchangeLog exchangeLog) {
		exchangeLogsDao.addAddExlogs(exchangeLog);
	}

	// 查询兑换商品
	public List<ExchangeLog> getAllExLogs(ExchangeLog exchangeLog) {
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		List<Object> objects = exchangeLogsDao.getAllExLogs(exchangeLog);
		for (Object object : objects) {
			ExchangeLog eg = (ExchangeLog) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeLogs.add(eg);
		}
		return exchangeLogs;
	}

	public List<ExchangeLog> getExLogsByshop(ExchangeLog exchangeLog) {
		List<Object> objects = exchangeLogsDao.getExLogsByshop(exchangeLog);
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		for (Object object : objects) {
			ExchangeLog eg = (ExchangeLog) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeLogs.add(eg);
		}
		return exchangeLogs;
	}

	public List<ExchangeLog> getExLogsByMember(ExchangeLog exchangeLog) {
		List<Object> objects = exchangeLogsDao.getExLogsByMember(exchangeLog);
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		for (Object object : objects) {
			ExchangeLog eg = (ExchangeLog) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeLogs.add(eg);
		}
		return exchangeLogs;
	}
	

	/**
	 * 根据企业的兑换记录中的会员
	 */
	public List<Long> findMemberIdFromExchangeLog(Long eaId){
		return exchangeLogsDao.findMemberIdFromExchangeLog(eaId);
	}
	
	/** 
	 * 查询会员在某企业中的兑换记录
	 */
	public List<ExchangeLog> getExLogsByEaAndMemberId(ExchangeLog exchangeLog) {
		List<Object> objects = exchangeLogsDao.getExLogsByEaAndMemberId(exchangeLog);
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		for (Object object : objects) {
			ExchangeLog eg = (ExchangeLog) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeLogs.add(eg);
		}
		return exchangeLogs;
	}
	
	/**
	 * 获取会员兑换同一件商品的次数
	 * @param goodId
	 * @param memberId
	 * @return int
	 */
	public int getCountMemberExchangeLogByGoodId(Long goodId, Long memberId) {
		return exchangeLogsDao.getCountMemberExchangeLogByGoodId(goodId, memberId);
	}

	public ExchangeLogsDao getExchangeLogsDao() {
		return exchangeLogsDao;
	}

	public void setExchangeLogsDao(ExchangeLogsDao exchangeLogsDao) {
		this.exchangeLogsDao = exchangeLogsDao;
	}
}
